package com.ls.io.rpc;

import com.ls.io.rpc.proxy.ProxyFactory;
import com.ls.io.rpc.service.Car;

import java.util.concurrent.CountDownLatch;

public class RpcClient {


    public static void main(String[] args) {
        int n = 50;
//        singleThread(n);
        multiThread(n);
    }

    public static void singleThread(int n){
        Car car = ProxyFactory.createProxy(Car.class);
        for(int i = 0; i < n; i++){
            String result = car.drive("xiaoming" + i, i);
            System.out.println("result:" + i + ":" + result);
        }
    }

    public static void multiThread(int n){
        CountDownLatch latch = new CountDownLatch(n);

        for(int i = 0; i < n; i++){
            final int index = i;
            new Thread(() ->{
                Car car = ProxyFactory.createProxy(Car.class);
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String result = car.drive("xiaoming" + index, index);
                System.out.println("result:" + index + ":" + result);
            }).start();

            latch.countDown();
        }

    }

}



